/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


(function( $ ){
    
    var methods = {
        draw:function(belaJson){
            if(belaJson==null) return this;
            bela.processBela(this, belaJson, null);
            return parent;
        }
    };
    
    bela = {
        "processBela":function(jqueryParent, belaJson) {
            var p = jqueryParent;
            var b = belaJson;
            $.each(b, function(key, value){
                bela.process(key,value,b,p,b);
            });
        },
        "process":function(key,value,jsonParent,jqueryParent,belaJson){
            $.each(bela.elementInit, function(i,pro){
                pro(key,value,jsonParent,jqueryParent,belaJson);
            });
            
            $.each(bela.prep, function(i,pro){
                pro(key,value,jsonParent,jqueryParent,belaJson);
            });
            if(value.isElement){
                var tag = value.tag;
                var htmlEle = document.createElement(tag);
                $.each(bela.ip, function(i,pro){
                    pro(htmlEle, key,value,jsonParent,jqueryParent,belaJson);
                });
                $(htmlEle).appendTo(jqueryParent);
            }
            $.each(bela.postp, function(i,pro){
                pro($(htmlEle), key,value,jsonParent,jqueryParent,belaJson);
            });
            delete value.tag;
            delete value.isElement;
            var njqp = $(htmlEle);
            var njsonp = value;
            if(typeof value == "object" && !(value instanceof Array) && !(value.isProcessed)){
                $.each(value, function(k,v){
                    if(!(v.isProcessed)){
                        bela.process(k,v, njsonp, njqp, belaJson);
                    }
                });
            }
            $.each(bela.finalize, function(i,pro){
                pro($(htmlEle), key,value,jsonParent,jqueryParent,belaJson);
            });
            value.isProcessed = true;
        },
        "elementInit":{},
        "prep":{
            "elementProcessor":function(key,value,jsonParent,jqueryParent,belaJson){
                if(bela.elementRegex.test(key)){
                    value.isElement = true;
                    value.tag = /^\w+[\||#|%]/.test(key) ? key.match(/^([^\|#%]+)/)[0] : "div";
                    value["@id"] = /#\w+\.?/.test(key) ? key.match(/#[^\.]+/)[0].slice(1) : "bela_" + (ecounter++);
                    var c = /\.\w+\.?/.test(key) ? key.match(/\.[^\.]+/g).join(" ").replace(/\./g, "") : null;
                    if(c) value["@class"] = c;
                    var t = /\|\w+[\.#]?/.test(key) ? key.match(/\|[^\.#]+/)[0].slice(1) : null;
                    if(t) value["@type"] = t;
                }
            },
            "attrProcessor":function(key,value,jsonParent,jqueryParent,belaJson){
                if(key.charAt(0)=="@"){
                    jqueryParent.attr(key.slice(1),value);
                }
            }
        },
        "ip":{
            "inputTypeProcessor":function(htmlEle, key,value,jsonParent,jqueryParent,belaJson){
                if(value.tag == "input" && value["@type"]){
                    htmlEle.type = value["@type"];
                    delete value["@type"];
                }
            },
            "idProcessor":function(htmlEle, key,value,jsonParent,jqueryParent,belaJson){
                htmlEle.id = value["@id"];
                delete value["@id"];
            }
        },
        "postp":{
            "inputProcessor":function(jqueryElement, key,value,jsonParent,jqueryParent,belaJson){
                var domele = jqueryElement[0];
                if(domele && domele.tagName.toLowerCase()=="input"){
                    if(!jqueryElement.attr("name")){
                        jqueryElement.attr("name",domele.id);
                    }
                    if((domele.type.toLowerCase()=="button" || domele.type.toLowerCase()=="submit" || domele.type.toLowerCase()=="reset") && !jqueryElement.attr("value")){
                        jqueryElement.attr("value",domele.id);
                    }
                }
            }
        },
        "finalize":{},
        "ecounter":0,
        "elementRegex":/^[\w+[\|\w+]*[%\w+]*]*#\w+[.\w+]*$/i
    };
    
    $.fn.bela = function(method) {
        // Method calling logic
        if ( methods[method] ) {
            return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
        } else if ( typeof method === 'object' || ! method ) {
            return methods.draw.apply( this, arguments );
        } else {
            $.error( 'Method ' +  method + ' does not exist on jQuery.tooltip' );
        } 
    };

})( jQuery );


